/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.nifi.processors.huawei.credentials.provider.factory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import com.obs.services.IObsCredentialsProvider;
import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.components.ValidationContext;
import org.apache.nifi.components.ValidationResult;
import org.apache.nifi.processors.huawei.credentials.provider.factory.strategies.AccessKeyPairCredentialsStrategy;


/**
 * Generates HuaweiCloud credentials in the form of HuaweiCredentialsProvider implementations for processors
 * and controller services.  The factory supports a number of strategies for specifying and validating
 * HuaweiCloud credentials, interpreted as an ordered list of most-preferred to least-preferred.  It also supports
 * derived credential strategies like Assume Role, which require a primary credential as an input.
 *
 * Additional strategies should implement CredentialsStrategy, then be added to the strategies list in the
 * constructor.
 *
 * @see org.apache.nifi.processors.huawei.credentials.provider.factory.strategies
 */
public class CredentialsProviderFactory {

    /**
     * Validates HuaweiCloud credential properties against the configured strategies to report any validation errors.
     * @return Validation errors
     */
    public Collection<ValidationResult> validate(final ValidationContext validationContext) {
        final CredentialsStrategy selectedStrategy = new AccessKeyPairCredentialsStrategy();
        final ArrayList<ValidationResult> validationFailureResults = new ArrayList<ValidationResult>();
        final Collection<ValidationResult> strategyValidationFailures = selectedStrategy.validate(validationContext);
        if (strategyValidationFailures != null) {
            validationFailureResults.addAll(strategyValidationFailures);
        }
        return validationFailureResults;
    }

    /**
     * Produces the HuaweiCredentialsProvider according to the given property set and the strategies configured in
     * the factory.
     * @return HuaweiCredentialsProvider implementation
     */
    public IObsCredentialsProvider getCredentialsProvider(final Map<PropertyDescriptor, String> properties) {
        final CredentialsStrategy primaryStrategy = new AccessKeyPairCredentialsStrategy();
        return primaryStrategy.getCredentialsProvider(properties);
    }
}
